//
//  ViewController.m
//  ZipDemo
//
//  Created by shenhongbang on 16/7/16.
//  Copyright © 2016年 中移(杭州)信息技术有限公司. All rights reserved.
//

#import "ViewController.h"
#import "SHBManager.h"
#import <SSZipArchive.h>
#import <Lottie/Lottie.h>
#import <AFNetworking.h>

@interface ViewController ()

@property (nonatomic, strong) LOTAnimationView *lottieLogo;
@property (weak, nonatomic) IBOutlet UILabel *label;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
//    self.view.backgroundColor = [UIColor whiteColor];

    SHBManager *manager = [SHBManager defaultManager];
    manager.destinationPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true) firstObject] stringByAppendingPathComponent:@"bbb"];

//    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://47.94.210.50/images.zip"]];
//    AFHTTPSessionManager *_manager = [[AFHTTPSessionManager alloc] init];
//    NSURLSessionDownloadTask *downTask = [_manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
//        NSLog(@"%@",downloadProgress.localizedDescription);
//    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
//
//        NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:response.suggestedFilename];
//        NSURL *url = [NSURL fileURLWithPath:filePath];
//        return url;
//    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
//
//        if (error == nil) {
//            NSLog(@"");
//        } else {
//            NSLog(@"");
//        }
//    }];
//
//    [downTask resume];

//    NSString * ss = [NSString stringWithFormat:@"%@/%@",manager.destinationPath,@"happchristmas"];
//    if ([self isFilePathExist:ss]) {
//        self.label.text= @"文件存在";
//        self.lottieLogo = [LOTAnimationView animationWithFilePath:[NSString stringWithFormat:@"%@/%@",ss,@"LottieLogodemo.json"]];
//        self.lottieLogo.frame = self.view.bounds;
//        [self.view insertSubview:self.lottieLogo atIndex:0];
//        [self.lottieLogo play];
//
//    }else{
//        self.label.text= @"文件不存在";
//        [manager unzipUrlStr:@"http://47.94.210.50/anim.zip" complete:^(NSString *filePath, NSError *error) {
//            if (error == nil) {
//                NSLog(@"filePath:%@", filePath);
//                NSString * strPath = [NSString stringWithFormat:@"%@/%@",filePath,@"happchristmas/LottieLogodemo.json"];
//                self.lottieLogo = [LOTAnimationView animationWithFilePath:strPath];
//                self.lottieLogo.frame = self.view.bounds;
//                [self.view insertSubview:self.lottieLogo atIndex:0];
//                [self.lottieLogo play];
//            } else {
//                NSLog(@"error:%@", error);
//            }
//        }];
//
//    }


//    NSString * strPath = @"/var/mobile/Containers/Data/Application/99DBA1C0-DE28-426E-9929-C0571F5EF929/Documents/bbb/anim/images/LottieLogodemo.json";
//    if (![self isFileExist:strPath]) {
//        return;
//    }
//    self.lottieLogo = [LOTAnimationView animationWithFilePath:strPath];
//    [self.view insertSubview:self.lottieLogo atIndex:0];
//    [self.lottieLogo play];

    self.lottieLogo = [LOTAnimationView animationNamed:@"data"];
    self.lottieLogo.contentMode = UIViewContentModeScaleAspectFill;
    self.lottieLogo.frame = self.view.frame;
    [self.view insertSubview:self.lottieLogo atIndex:0];

}
-(BOOL) isFilePathExist:(NSString *)path
{

    NSString *filePath = path;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL result = [fileManager fileExistsAtPath:filePath];
    NSLog(@"这个文件已经存在：%@",result?@"是的":@"不存在");
    return result;
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];

 self.lottieLogo.frame = self.view.frame;
}
- (IBAction)action:(id)sender {
    self.lottieLogo = [LOTAnimationView animationNamed:@"data"];
    self.lottieLogo.contentMode = UIViewContentModeScaleAspectFill;
    self.lottieLogo.frame = self.view.frame;
    [self.view insertSubview:self.lottieLogo atIndex:0];
    [self.lottieLogo playWithCompletion:^(BOOL animationFinished) {

    }];
}

-(BOOL) isFileExist:(NSString *)fileName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    NSString *filePath = [path stringByAppendingPathComponent:fileName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL result = [fileManager fileExistsAtPath:filePath];
    NSLog(@"这个文件已经存在：%@",result?@"是的":@"不存在");
    return result;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

     [self.lottieLogo playWithCompletion:^(BOOL animationFinished) {
         NSLog(@"success");
     }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
